
// A1b_summarize_mc
//==============================================================================

// Description: This file formats and summarizes the estimates for the marginal cost

clear
eststo clear
set more off

cd "D:\data_replication"


// Import and format data: EU only
//------------------------------------------------------------------------------

eststo clear
use statistics_and_graphs\costs\costs_summary.dta, clear
keep if mc_flag == 1

keep if partner == 1 | partner == 3 | partner == 4 | partner == 5 | partner == 6 | ///
partner == 7 | partner == 8 | partner == 9 | partner == 10 | partner == 11 | ///
partner == 17 | partner == 18 | partner == 30 | partner == 32 | partner == 38 | /// 
partner == 46 | partner == 53 | partner == 54 | partner == 55 | partner == 60 | /// 
partner == 61 | partner == 66 | partner == 68 | partner == 91 | partner == 600  

gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg mc_opt i.partner, fe

esttab using statistics_and_graphs\costs\output_files\mc_opt_partner_EU.csv, replace wide label nostar ///
title(Regression table\label{tab1})


// Import and format data: All countries
//------------------------------------------------------------------------------

eststo clear
use statistics_and_graphs\costs\costs_summary.dta, clear
keep if mc_flag == 1

gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg mc_opt i.partner, fe

esttab using statistics_and_graphs\costs\output_files\mc_opt_partner_all.csv, replace wide label nostar ///
title(Regression table\label{tab1})


import delimited statistics_and_graphs\costs\output_files\mc_opt_partner_all.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n >  189
drop v3

destring v1, replace
destring v2, replace

rename v1 partner
rename v2 mc
gen year = 2010
merge 1:1 partner year using data\GDP\GDP_constant_prices_data.dta
keep if _merge == 3
drop _merge


// Keep only 20 largest countries and save
//------------------------------------------------------------------------------

sort mc
drop if gdp < 470

export delimited statistics_and_graphs\costs\output_files\mc_opt_partner_top20.csv, replace




